//
//  YbrRecordBgkMusic.m
//  wifibox
//
//  Created by xinle on 15/8/24.
//  Copyright (c) 2015年 yinbaner. All rights reserved.
//

#import "YbrRecordBgkMusicDao.h"

@implementation YbrRecordBgkMusicDao

+(BOOL)insert:(YbrMusicItem *)item
{
    __block BOOL isOk;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        
        isOk = [db executeUpdate:@"insert into record_bgkMusic (songName ,music_id, songUrl,isSel) VALUES (?,?,?,?)", item.musicName ,[NSNumber numberWithInt:item.musicId],item.file,[NSNumber numberWithInt:item.isSel]];
    }];
    return isOk;
}

+(NSArray *)query
{
    __block NSArray *arr;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from record_bgkMusic"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrMusicItem *item = [[YbrMusicItem alloc] init];
            item._id = [rs intForColumn:@"_id"];
            item.musicName = [rs stringForColumn:@"songName"];
            item.musicId = [rs intForColumn:@"music_id"];
            item.file = [rs stringForColumn:@"songUrl"];
            item.isSel = [rs intForColumn:@"isSel"];
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;

}

+(int) getMusicUrl:(NSString *) file
{
    __block int count;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select count(*) from record_bgkMusic where songUrl = %@",file];
        while (rs.next) {
            count = [rs intForColumn:@"count(*)"];
        }
        [rs close];
    }];
    return count;
    
}

+(BOOL) delBgkMusicById:(int)muiscId{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from record_bgkMusic where music_id=%i",muiscId];
    }];
    return isOk;
}

+(BOOL)updateWithIsSel:(int)isSel Id:(int)Id
{
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"update record_bgkMusic set isSel=%i where _id=%i",isSel,Id];
    }];
    return isOk;

}

+(BOOL)updateSel
{
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"update record_bgkMusic set isSel=%i",0];
    }];
    return isOk;
    
}

+(int)queryWithIsSelWithId:(int)musicId
{
    __block int count;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select isSel from record_bgkMusic where music_id = %i",musicId];
        while (rs.next) {
            count = [rs intForColumn:@"isSel"];
        }
        [rs close];
    }];
    return count;

}

@end
